<?php
require_once ('youtubevideo.php');
require_once ('youtubevideoauthor.php');
require_once ('youtubevideoinformation.php');
require_once ('youtubebasics.php');

/**

	YoutubeParser Class to parse the feed from Youtube's API
*/

class YoutubeParser{
	var $xml = null;
	var $videos = array();
	var $searchResults = null;
	function __construct ( $data ){
		$this->xml = new SimpleXMLElement( $data );
		$this->searchResults = (array)$this->xml->children ("http://a9.com/-/spec/opensearchrss/1.0/");
		if (  !isset( $this->xml->entry ) ) {
			// we are reading a simple video entry
			$this->videos[] = $this->_readEntry( $this->xml );
			return;
		} else{	
			foreach ( $this->xml->entry as $entry ){
				$this->videos[] = $this->_readEntry( $entry );
				}
		}

	}

	function _readEntry ( $entry ){
			$video = new YoutubeVideo;
			$video->id = (string)$entry->id;
			$video->title = (string)$entry->title;
			$video->content = (string)$entry->content;
			$video->published = (string)$entry->published;
			$video->updated = (string)$entry->updated;
			$video->link = (string)$this->_parseLink ( $entry );
			$video->author = $this->_parseAuthor( $entry );
			$video->information = $this->_parseVideoInformation( $entry );
			$video->statistic = $this->_parseVideoStatistic( $entry );
			$video->categories = $this->_parseCategories( $entry );
			return $video;
	}

	function _parseLink( $entry ){
				foreach ($entry->link as $link ){
				$attrs = $link->attributes();
				if ( $attrs['rel'] == 'alternate' ){
						return $attrs['href'];
				}
			}
	}
	function _parseCategories( $entry ){
		$categories = array();
		$c=0;
		foreach ($entry->category as $category ){
			$attrs = $category->attributes();
			if ( (string)$attrs['scheme'] == 'http://schemas.google.com/g/2005#kind' ) continue;
			$categories[$c] = (string)$attrs['term'];
			$c++;
		}
		return $categories;
	}
	function _parseAuthor( $entry ){
		$author = new YoutubeVideoAuthor;
		$author->name = (string)$entry->author->name;
		$author->uri = (string)$entry->author->uri;
		return $author;
	}

	function _parseVideoInformation( $entry ){
		$media = $entry->children('http://search.yahoo.com/mrss/');
		$information = new YoutubeVideoInformation;
		$group = $media->group;
		$information->mediaTitle = (string) $group->title;
		$information->mediaDescription = (string) $group->description;
		foreach ( $group->content as $content ){
			$attrs = $content->attributes();
			switch ( $attrs ['type']  ){
				case 'application/x-shockwave-flash':
					$information->mediaType = (string)$attrs ['type'];
					$information->mediaUrl = (string)$attrs ['url'];
					$information->mediaDuration = (string)$attrs ['duration'];
					break;
				
				case 'video/3gpp':
					$information->movileType = (string)$attrs ['type'];
					$information->movileUrl = (string)$attrs ['url'];
					$information->movileDuration = (string)$attrs ['duration'];
					break;
				
				
			}
		}
		$tc = 0;
		foreach ( $group->thumbnail as $thumb ){
			$attrs = $thumb->attributes();
			$information->thumbnails[$tc] = array ( 'url' => (string)$attrs['url'], 'width' => (string)$attrs['width'], 'height' => (string)$attrs['height'], 'time' => (string)$attrs['time'] );
			$tc++;
		}
		return $information;
	}

	/**
		TODO: complete this method
	*/
	function _parseVideoStatistic( $entry ){
		$yvs = new YoutubeVideoStatistic;
		$gd = $entry->children ('http://schemas.google.com/g/2005');
		if ( isset ( $gd->comments ) ){
			$comment = $gd->comments->feedLink->attributes();
			$yvs->commentsCount = (string)$comment['countHint'];
			$yvs->commentsFeed = (string)$comment['href'];
		}
		if ( isset ( $gd->rating ) ){
			$rating = $gd->rating->attributes();
			$yvs->ratingCount = (string)$rating['numRaters'];
			$yvs->ratingAverage = (string)$rating['average'];
			$yvs->ratingMin = (string)$rating['min'];
			$yvs->ratingMax = (string)$rating['max'];
		}
		$yt = $entry->children ('http://gdata.youtube.com/schemas/2007');
		if ( isset ( $yt->statistics ) ){
			$statistics = $yt->statistics->attributes();
			$yvs->viewCount = (string)$statistics['viewCount'];
			$yvs->favoriteCount = (string)$statistics['favoriteCount'];
		}
		return $yvs;
		/*foreach ( $gd as $gde ){
			YoutubeBasics::debug ( $gde->attributes() );
		}*/
	}

	function videos(){
		return $this->videos;
	}

	function searchResults(){
		return $this->searchResults;
	}
	
};

?>
